﻿#region Using
/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-12-3
Description:Agency_List.aspx.cs页面代码
=============================================================================	
*/

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;
using FineUI;
using FTchina.SearchQuery;
using FTchina.Library;
using FTchina;
using PWMIS.DataMap.Entity;
using FTchina.Logic;
#endregion
public partial class Agency_List_cs : BasePage
{


    string ReturnPage = "Agency_List.aspx";

    FTchina.SearchQuery.QueryModel qm = new FTchina.SearchQuery.QueryModel();//实例化搜索模型
    string WindowsPage = "Agency_Listaction.aspx";//弹出窗口的页面地址
    PWMIS.DataProvider.Data.AdoHelper ado = PWMIS.DataProvider.Adapter.MyDB.GetDBHelperByConnectionName("DYDB");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (MasterHelper.BssAreaCode.Trim().Length == 0)
        {
            Response.Write("请联系管理员配置" + MasterHelper.ParentItemName + "关联AREA_CODE");
            Response.End();
        }
        
        if (!IsPostBack)
        {
            btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
            btnNew.OnClientClick = Window1.GetShowReference(WindowsPage, "新增");
            if (qm.WorkPage != ReturnPage)
            {

                qm.WorkPage = ReturnPage;
                qm.Key1 = string.Empty;
            }
            BindGrid(qm);
        }


        if (MasterHelper.UserGroupName.Contains("管理员"))
        {
            btnNew.Enabled = true;             
            btnDelete.Enabled = true;
        }
        else
        {
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
        }
    }


    #region 绑定列表
    private void BindGrid(QueryModel qm)
    {
        BindGrid(qm, false);
    }

    /// <summary>
    /// 绑定表格
    /// </summary>
    private void BindGrid(QueryModel qm, bool IsToExcel)
    {
         
        #region 原来的
        ////每页条数
        //Grid1.PageSize = 12;

        //FTchina.Models.Agency_List mAgency_List = new FTchina.Models.Agency_List();

        ////拼接查询条件
        //OQLCompare cmp = new OQLCompare(mAgency_List);
        //OQLCompare cmpResult = (
        //     (cmp.Comparer(mAgency_List.AgencyName, OQLCompare.CompareType.Like, "%" + qm.Key1 + "%")
        //    | cmp.Comparer(mAgency_List.AgencyAddress, OQLCompare.CompareType.Like, "%" + qm.Key1 + "%")
        //    | cmp.Comparer(mAgency_List.BSSCode, OQLCompare.CompareType.Like, "%" + qm.Key1 + "%")
        //    | cmp.Comparer(mAgency_List.ContactMan, OQLCompare.CompareType.Like, "%" + qm.Key1 + "%")
        //    | cmp.Comparer(mAgency_List.ContactTel1, OQLCompare.CompareType.Like, "%" + qm.Key1 + "%")
        //    | cmp.Comparer(mAgency_List.ContactTel2, OQLCompare.CompareType.Like, "%" + qm.Key1 + "%")
        //    | cmp.Comparer(mAgency_List.AgencyManager, OQLCompare.CompareType.Like, "%" + qm.Key1 + "%"))
        //    & (cmp.Comparer(mAgency_List.AgencyUnit, OQLCompare.CompareType.IN, MasterHelper.BssAreaCode))
        //    );
        
        //OQL q = new OQL(mAgency_List);
      
        //q.Select().Where(cmpResult).OrderBy(mAgency_List.Id, "desc");//按条件查询条件

        //int count = EntityQuery<FTchina.Models.Agency_List>.QueryList(q, mAgency_List).Count;//获取分页前的条数

        //if (IsToExcel)
        //{//如果是导出Excel
        //    FTchina.OfficeHelper.ToExcel.IListToExcel(EntityQuery<FTchina.Models.Agency_List>.QueryList(q, mAgency_List));
        //    return;
        //}

        //q = FTchina.Logic.PageLogic.MakeSubPage(q, Grid1, count);//分页       
        //List<FTchina.Models.Agency_List> list = EntityQuery<FTchina.Models.Agency_List>.QueryList(q, mAgency_List);//分页后绑定数据源

        //Grid1.RecordCount = count;
        //Grid1.DataSource = list;
        //Grid1.DataBind();

        #endregion


        //每页条数
        Grid1.PageSize = 12;

        FTchina.Models.BSSAgencyList mBSSAgencyList = new FTchina.Models.BSSAgencyList();

        //拼接查询条件
        OQLCompare cmp = new OQLCompare(mBSSAgencyList);
        OQLCompare cmpResult = (
           
            cmp.Comparer(mBSSAgencyList.AREA_CODE, OQLCompare.CompareType.IN, MasterHelper.BssAreaCode)
            );

        OQL q = new OQL(mBSSAgencyList);

        q.Select().Where(cmpResult).OrderBy(mBSSAgencyList.DEPART_ID, "desc");//按条件查询条件

        int count = EntityQuery<FTchina.Models.BSSAgencyList>.QueryList(q, ado).Count;//获取分页前的条数
        q.PageWithAllRecordCount = count;
        if (IsToExcel)
        {//如果是导出Excel
            FTchina.OfficeHelper.ToExcel.IListToExcel(EntityQuery<FTchina.Models.BSSAgencyList>.QueryList(q, ado));
            return;
        }

        q = FTchina.Logic.PageLogic.MakeSubPage(q, Grid1, count);//分页

        List<FTchina.Models.BSSAgencyList> list = EntityQuery<FTchina.Models.BSSAgencyList>.QueryList(q, ado);//分页后绑定数据源

        Grid1.RecordCount = count;
        Grid1.DataSource = list;
        Grid1.DataBind();



        if (qm.Key1.Length > 0)
        {
            ttbSearch.Text = qm.Key1;
            ttbSearch.ShowTrigger1 = true;
        }

    }
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Grid1_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {

        Grid1.PageIndex = e.NewPageIndex;
        BindGrid(qm);
    }
    /// <summary>
    /// 删除按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        foreach (int row in Grid1.SelectedRowIndexArray)
        {
            sb.Append(Grid1.DataKeys[row][0].ToString());
            sb.Append(",");
        }
        foreach (string ids in sb.ToString().TrimEnd(',').Split(','))
        {

            FTchina.BLL.Agency_ListManager.DeleteAgency_ListById(Convert.ToInt32(ids));
        }
        BindGrid(qm);
        Grid1.SelectedRowIndexArray = new int[] { };//去除默认的选项
    }


    /// <summary>
    /// 弹出窗口返回的处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Window1_Close(object sender, EventArgs e)
    {
        BindGrid(qm);
    }
    #endregion

    #region 搜索过滤
    /// <summary>
    /// 点击搜索上的关闭按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger1Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = false;
        ttbSearch.Text = String.Empty;//清空搜索字符串
        qm.Key1 = String.Empty;//清空搜索条件
        BindGrid(qm);
    }
    /// <summary>
    /// 点击搜索按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger2Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = true;//显示关闭搜索按钮
        if (ttbSearch.Text.Trim().Length > 0)
        {
            qm.Key1 = ttbSearch.Text.Trim();
        }
        else
        {
            qm.Key1 = "";
        }
        BindGrid(qm);
    }


    #endregion


    #region 导出Excel
    /// <summary>
    /// 带过滤条件的导出，不分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_click(object sender, EventArgs e)
    {
        BindGrid(qm, true);
    }
    #endregion




    #region GRID显示放号情况

    protected void Grid1_RowClick(object sender, FineUI.GridRowClickEventArgs e)
    {
        ShowState();
    }

    protected void ShowState()
    {
        string bsscode =  Grid1.DataKeys[Grid1.SelectedRowIndex][1].ToString();
         
        PWMIS.DataProvider.Data.AdoHelper dydb=PWMIS.DataProvider.Adapter.MyDB.GetDBHelperByConnectionName("DYDB");

        //string sql = "select * from bssagencycountlist t where t.depart_code='" + bsscode + "' ";
        //LogHelper.Debug(sql);
        //DataTable dt = dydb.ExecuteDataSet(sql).Tables[0];
        List<BSSAgencyCount> bclist = new List<BSSAgencyCount>();

        int year = DateTime.Now.Year;
        for (int i = year; i >= year-2; i--)
        {
            BSSAgencyCount bc = new BSSAgencyCount();
            bc.BssCode = bsscode;
            bc.Year = i.ToString();
            bc.M1 = getCountByBssCode(bc.BssCode, bc.Year + "01",dydb);
            bc.M2 = getCountByBssCode(bc.BssCode, bc.Year + "02", dydb);
            bc.M3 = getCountByBssCode(bc.BssCode, bc.Year + "03", dydb);
            bc.M4 = getCountByBssCode(bc.BssCode, bc.Year + "04", dydb);
            bc.M5 = getCountByBssCode(bc.BssCode, bc.Year + "05", dydb);
            bc.M6 = getCountByBssCode(bc.BssCode, bc.Year + "06", dydb);
            bc.M7 = getCountByBssCode(bc.BssCode, bc.Year + "07", dydb);
            bc.M8 = getCountByBssCode(bc.BssCode, bc.Year + "08", dydb);
            bc.M9 = getCountByBssCode(bc.BssCode, bc.Year + "09", dydb);
            bc.M10 = getCountByBssCode(bc.BssCode, bc.Year + "10", dydb);
            bc.M11 = getCountByBssCode(bc.BssCode, bc.Year + "11", dydb);
            bc.M12 = getCountByBssCode(bc.BssCode, bc.Year + "12", dydb);
            bc.Sum = getSumByBssCode(bc.BssCode, bc.Year, dydb);
            bclist.Add(bc);
        }
        Grid2.RecordCount = bclist.Count;
        Grid2.DataSource = bclist;
        Grid2.DataBind();

    }
    /// <summary>
    /// 获取代理商一月发展量
    /// </summary>
    /// <param name="bsscode"></param>
    /// <param name="YearMonth"></param>
    /// <returns></returns>
    private string getCountByBssCode(string bsscode, string YearMonth, PWMIS.DataProvider.Data.AdoHelper dydb)
    {
        string sql = "select nvl(sum(count),0) as count  from bssagencycountlist t where t.depart_code='" + bsscode + "' and mon='" + YearMonth + "'  ";
        return dydb.ExecuteScalar(sql).ToString();
    }
    /// <summary>
    /// 获取代理商一年的发展量
    /// </summary>
    /// <param name="bsscode"></param>
    /// <param name="YearMonth"></param>
    /// <param name="dydb"></param>
    /// <returns></returns>
    private string getSumByBssCode(string bsscode, string YearMonth, PWMIS.DataProvider.Data.AdoHelper dydb)
    {
        string sql = "select nvl(sum(count),0) as count  from bssagencycountlist t where t.depart_code='" + bsscode + "' and SUBSTR(mon,0,4)=" + YearMonth + "  ";
        return dydb.ExecuteScalar(sql).ToString();
    }
    class BSSAgencyCount
    {
        public string BssCode { get; set; }
        public string Year { get; set; }
        public string M1 { get; set; }
        public string M2 { get; set; }
        public string M3 { get; set; }
        public string M4 { get; set; }
        public string M5 { get; set; }
        public string M6 { get; set; }
        public string M7 { get; set; }
        public string M8 { get; set; }
        public string M9 { get; set; }
        public string M10 { get; set; }
        public string M11 { get; set; }
        public string M12 { get; set; }
        public string Sum { get; set; }
    }

    #endregion
}






